﻿//Dùng tại frmTongHopDT: xem toàn bộ công ty (MaDV == '%')

using System;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using prjKQXS_DLL;
using prjKQXS_DLL.Doanh_thu;

namespace prjKQXS_Report
{
    public partial class rptMauHH03x : DevExpress.XtraReports.UI.XtraReport
    {
        //Cài đặt event
        public delegate void OnLoadCompleteHandler();
        public event OnLoadCompleteHandler OnLoadComplete;

        dsReports dsReports1 = new dsReports();

        #region rpt ============================================================================
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tuNgay"></param>
        /// <param name="denNgay"></param>
        /// <param name="TenCanBo"></param>
        /// <param name="tenDonVi"></param>
        /// <param name="maDonVi">Giá trị là "TC" hoặc mã đơn vị tương ứng</param>
        public rptMauHH03x()
        {
            InitializeComponent();
        }

        private void loadSource()
        {
            fillHeader(dsReports1);

            Report_DAL report_dal = new Report_DAL();
            dsReports1 = report_dal.reportDungChung(dsReports1);

            clsDoanhThu cls = new clsDoanhThu();
            cls.OnAction += new clsDoanhThu.OnActionHandler(cls_OnAction);
            cls.OnError += new clsDoanhThu.OnErrorHandler(cls_OnError);

            string slblSumHT2 = null;
            dsReports1.Tables.Remove("rptHoaHong");
            dsReports1.Tables.Add(cls.loadData_rptMauHH03x(((DateTime)pTuNgay.Value), ((DateTime)pDenNgay.Value), ref slblSumHT2));

            this.DataSource = dsReports1;
            this.DataMember = "rptHoaHong";
        }

        private void rptMauHH03x_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            loadSource();

            if (pFormName.Value.ToString().Trim() == "frmTongHopDoanhThu")
            {
                xrlblMau.Text = "Mẫu HH-03A";

                xrtblDoanhThu1.DeleteColumn(xrtcLoTo);
                xrtblDoanhThu1.DeleteColumn(xrtcBoc);
                xrtcTruyenThong.Text = "DOANH THU";

                xrtblDoanhThu1.DeleteColumn(xrtcTruyenThong_HH1);
                xrtblDoanhThu1.DeleteColumn(xrtcBoc_HH1);
                xrtblDoanhThu1.DeleteColumn(xrtcLoTo234_HH1);

                xrtcTruyenThong_DT1.Text = "T.THỐNG";
                xrtcBoc_DT1.Text = "BÓC";
                xrtcLoTo234_DT1.Text = "LÔ TÔ";

                xrtblDoanhThu2.DeleteColumn(xrtcTruyenThong_HH2);
                xrtblDoanhThu2.DeleteColumn(xrtcBoc_HH2);
                xrtblDoanhThu2.DeleteColumn(xrtcLoTo234_HH2);

                xrtblDoanhThu3.DeleteColumn(xrtcTruyenThong_HH3);
                xrtblDoanhThu3.DeleteColumn(xrtcBoc_HH3);
                xrtblDoanhThu3.DeleteColumn(xrtcLoTo234_HH3);


                #region Điều chỉnh kích thước ------------------------
                float width = 200;
                xrtblDonVi1.WidthF += width / 2;
                xrtblDonVi2.WidthF += width / 2;
                xrtblDonVi3.WidthF += width / 2;

                xrtcTenDV1.WidthF += 10;

                xrtblDoanhThu1.WidthF -= width;
                xrtblDoanhThu2.WidthF -= width;
                xrtblDoanhThu3.WidthF -= width;

                xrtblDoanhThu1.LeftF += width / 2;
                xrtblDoanhThu2.LeftF += width / 2;
                xrtblDoanhThu3.LeftF += width / 2;

                xrtblTongDT1.LeftF -= width / 2;
                xrtblTongDT2.LeftF -= width / 2;
                xrtblTongDT3.LeftF -= width / 2;

                xrtblTongDT1.WidthF += width / 2;
                xrtblTongDT2.WidthF += width / 2;
                xrtblTongDT3.WidthF += width / 2;
                #endregion Điều chỉnh kích thước ---------------------
            }
            else
            {
                xrlblMau.Text = "Mẫu HH-03B";
            }
        }

        private void rptMauHH03x_AfterPrint(object sender, EventArgs e)
        {
            OnLoadComplete();
        }
        #endregion rpt =========================================================================
        #region cls event --------------------------------------------
        private void cls_OnAction(SqlException e)
        {
            cls_OnError(e);
        }

        private void cls_OnError(Exception e)
        {
            XtraMessageBox.Show("Một lỗi chưa được xử lý đã xảy ra. Chi tiết lỗi: \n" 
                + e.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            this.ClosePreview();
        }
        #endregion ---------------------------------------------------

        private void fillHeader(dsReports dsReports1)
        {
            dsReports1.rptHoaHongByDaiLy1.Rows.Clear();
            if (pMaDV.Value.ToString().Trim() != "%")
            {
                if (!string.IsNullOrEmpty(pTenDV.Value.ToString().Trim()))
                {
                    lblDonVi.Text = "PHÒNG XSKT " + pTenDV.Value.ToString().Trim().ToUpper();
                }
            }

            if (((DateTime)pTuNgay.Value).Date.CompareTo(((DateTime)pDenNgay.Value).Date) == 0)
            {
                lblNgayThang.Text = "Ngày " + ((DateTime)pTuNgay.Value).ToString("dd/MM/yyyy");
            }
            else
            {
                lblNgayThang.Text = "Từ ngày " + ((DateTime)pTuNgay.Value).ToString("dd/MM/yyyy") 
                    + "  đến ngày " + ((DateTime)pDenNgay.Value).ToString("dd/MM/yyyy");
            }
        }

        int stt = 0;
        private void GroupHeader1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrtcSTT2.Text = (++stt).ToString();
        }

        #region Tính %HT ================================================================
        decimal dt = 0;
        decimal kh = 0;
        private void xrtcHT_SummaryReset(object sender, EventArgs e)
        {
            dt = 0;
            kh = 0;
        }

        private void xrtcHT_SummaryRowChanged(object sender, EventArgs e)
        {
            dt += GetCurrentColumnValue("TongDT") == DBNull.Value ? 0 : Convert.ToInt32(GetCurrentColumnValue("TongDT"));
            kh += GetCurrentColumnValue("DoanhThu") == DBNull.Value ? 0 : Convert.ToInt32(GetCurrentColumnValue("DoanhThu"));
        }

        private void xrtcHT_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {
            e.Result = dt / (kh == 0 ? 1 : kh) * 100;
            e.Result = (decimal)e.Result == 0 ? DBNull.Value : e.Result;
            e.Handled = true;
        }
        #endregion Tính %HT =============================================================

        #region Tính Sum%HT =============================================================
        decimal sum_dt = 0;
        decimal sum_kh = 0;
        private void xrtcSumHT_SummaryReset(object sender, EventArgs e)
        {
            sum_dt = 0;
            sum_kh = 0;
        }

        private void xrtcSumHT_SummaryRowChanged(object sender, EventArgs e)
        {
            sum_dt += GetCurrentColumnValue("TongDT") == DBNull.Value ? 0 : Convert.ToInt32(GetCurrentColumnValue("TongDT"));
            sum_kh += GetCurrentColumnValue("DoanhThu") == DBNull.Value ? 0 : Convert.ToInt32(GetCurrentColumnValue("DoanhThu"));
        }

        private void xrtcSumHT_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {
            e.Result = sum_dt / (sum_kh == 0 ? 1 : sum_kh) * 100;
            e.Handled = true;
        }
        #endregion Tính Sum%HT ==========================================================



        #region Đoạn này để không in ra các tổng HH bằng 0 -------------------------------------
        //Dùng chung cho: xrtcTruyenThong_HH2, xrtcBoc_HH2, xrtcLoTo234_HH2, xrtcCapSo_HH2
        decimal HH_TruyenThong, HH_Boc, HH_LoTo234, HH_CapSo;

        private void summaryGetResult(object sender, SummaryGetResultEventArgs e)
        {
            string dataMember = ((XRTableCell)sender).DataBindings[0].DataMember;
            int dot = dataMember.IndexOf('.');
            dataMember = dataMember.Substring(dot + 1, dataMember.Length - dot - 1);

            decimal value = 0;
            if (dataMember == "HH_TruyenThong") value = HH_TruyenThong;
            else if (dataMember == "HH_Boc") value = HH_Boc;
            else if (dataMember == "HH_LoTo234") value = HH_LoTo234;
            else if (dataMember == "HH_CapSo") value = HH_CapSo;

            e.Handled = value == 0;
        }

        private void summaryReset(object sender, EventArgs e)
        {
            HH_TruyenThong = 0;
            HH_Boc = 0;
            HH_LoTo234 = 0;
            HH_CapSo = 0;
        }

        private void summaryRowChanged(object sender, EventArgs e)
        {
            HH_TruyenThong += clsCommon.getDecimal(GetCurrentColumnValue("HH_TruyenThong"));
            HH_Boc += clsCommon.getDecimal(GetCurrentColumnValue("HH_Boc"));
            HH_LoTo234 += clsCommon.getDecimal(GetCurrentColumnValue("HH_LoTo234"));
            HH_CapSo += clsCommon.getDecimal(GetCurrentColumnValue("HH_CapSo"));
        }
        #endregion -----------------------------------------------------------------------------
    }
}
